package com.fqgj.xjd.user.integration.ocr;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ·
 *
 * @author liuming
 * Created on 下午3:05 2018/9/11.
 */
public class HyperDocs {

    private static final Logger LOGGER = LoggerFactory.getLogger(HyperDocs.class);

//    private final String SERVER_PATH = "https://docs.hyperverge.co/v1-1/";
    private final String SERVER_PATH = "https://ind.docs.hyperverge.co/v2.0/";

    private final static String appId = "d3659a";

    private final static String appKey = "6398e1cf19f55942d7df";

    public enum InputType {
        IMAGE {
            @Override
            public String getInputTag() {
                return "image";
            }

            @Override
            public String getMIMEType() {
                return "image/*";
            }
        },
        PDF {
            @Override
            public String getInputTag() {
                return "pdf";
            }

            @Override
            public String getMIMEType() {
                return "application/pdf";
            }
        };

        public abstract String getInputTag();

        public abstract String getMIMEType();
    }

    public enum EndPoint {
        READ_KYC {
            //generuc endpoint, will work with PAN, PASSPORT & AADHAAR
            @Override
            public String getEndpointString() {
                return "readKYC";
            }
        },
        READ_PAN {
            //end point for PAN Cards
            @Override
            public String getEndpointString() {
                return "readPAN";
            }
        },
        READ_AADHAAR {
            //end point for Aadhaar
            @Override
            public String getEndpointString() {
                return "readAadhaar";
            }
        },
        READ_PASSPORT {
            //end point for Passports
            @Override
            public String getEndpointString() {
                return "readPassport";
            }

        };

        public abstract String getEndpointString();

    }

    /**
     * @param inputPath: local path of the input image/pdf on which OCR needs to be run
     * @param inputType: the type of input i.e. image or pdf
     * @param endPoint:  the API endpoint that should be used to run the OCR. This value is dependent on the type of document(PAN, Passport or Aadhaar)
     * @return String response from the server
     **/
    public String requestMethod(String inputPath, InputType inputType, EndPoint endPoint) {
        try {
            LOGGER.info("------ ocr request start, inputPath:{}, endPoint:{} ------", inputPath, endPoint);
            JSONObject headers = new JSONObject();
            headers.put("appId", appId);
            headers.put("appKey", appKey);
            HVMultipartPost multipartPost = new HVMultipartPost(SERVER_PATH + endPoint.getEndpointString(), headers);
            multipartPost.addFileEntity(inputType.getInputTag(), inputType.getMIMEType(), inputPath);
            String responseJson = multipartPost.executeRequest();
            LOGGER.info("------ ocr response json:{} ------", responseJson);
            return responseJson;
        } catch (Exception e) {
            LOGGER.error("------ ocr request exception, inputPath:{}, endpoint:{} ------", inputPath, endPoint, e);
        }
        return null;
    }
}
